package com.aaa.tmr.controller;

import com.aaa.tmr.entity.Menu;
import com.aaa.tmr.service.MenuService;
import com.aaa.tmr.util.Result;
import com.aaa.tmr.util.ReturnStatus;
import com.aaa.tmr.util.TreeUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 菜单权限表(Menu)表控制层
 *
 * @author makejava
 * @since 2021-03-17 15:30:46
 */
@Controller
@RequestMapping("menu")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne")
    @ResponseBody
    public Menu selectOne(Integer id) {
        return this.menuService.queryById(id);
    }
    /**
     * 跳转菜单分页列表
     * @return
     */
    @RequestMapping("toMenu")
    public String toPage(){
        return "back/menu-page";
    }

    /**
     * 菜单查询
     * @param menu
     * @return
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result queryAll(Menu menu){
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
               0L,menuService.queryAll(menu));
    }

    /**
     * 菜单添加
     * @param menu
     * @return
     */
    @ResponseBody
    @RequestMapping("insert")
    public Result insert(Menu menu){
        return new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0L,menuService.insert(menu));
    }

    /**
     * 菜单更新
     * @param menu
     * @return
     */
    @ResponseBody
    @PostMapping("update")
    public Result update(Menu menu){
        return  new Result(ReturnStatus.SUCCESS.getCode(), ReturnStatus.SUCCESS.getMessage(),
                0L,menuService.update(menu));
    }

    /**
     * 通过主键删除菜单
     * @param menuId
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer menuId){
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                0L,this.menuService.deleteById(menuId));
    }

    @ResponseBody
    @GetMapping("findParentNameId")
    public Result findParentNameId(Integer menuId){
        return new Result(ReturnStatus.SUCCESS.getCode(),ReturnStatus.SUCCESS.getMessage(),
                0L ,this.menuService.findParentNameId(menuId));
    }

    /**
     * 查询所有菜单，用于添加和编辑菜单时的生成树
     *
     * @return List
     * @createBy Enzo
     * @createTime 2020/12/29 22:54
     */
    @GetMapping("queryAllMenus")
    @ResponseBody
    public List queryAllMenus() {
        List<Menu> menus = menuService.queryAllMenuTree();
        return TreeUtil.fromMenuListToTreeList(menus);
    }


}